package org.openmore.consumer.controller.api;

import io.swagger.annotations.*;
import org.openmore.consumer.annotation.TokenAuthCheck;
import org.openmore.consumer.controller.common.BaseController;
import org.openmore.consumer.dto.common.BaseResponse;
import org.openmore.consumer.dto.common.LoginParams;
import org.openmore.consumer.dto.common.UserProfileDto;
import org.openmore.consumer.service.feign.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;

@Api(value = "/Account", tags = "Account", description = "账号相关接口")
@RequestMapping(value = "/api/account", produces = {APPLICATION_JSON_UTF8_VALUE})
@RestController
public class AccountController extends BaseController {

    @Autowired
    private AccountService accountService;

    @ApiOperation(value = "手机号短信登陆或手机号密码登录(app端deviceToken必传)", response = UserProfileDto.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = UserProfileDto.class)})
    @RequestMapping(method = RequestMethod.POST, value = "/{tenantId}/login")
    public BaseResponse login(@PathVariable @ApiParam(value = "应用ID") String tenantId,
                              @RequestBody @ApiParam(value = "登录信息") LoginParams params) {
        logger.debug(">> login");
        return BaseResponse.buildSuccessResponse(accountService.login(tenantId, params));
    }

    @TokenAuthCheck
    @ApiOperation(value = "退出登录", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = BaseResponse.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/logout")
    public BaseResponse logout() {
        logger.debug(">> logout");
        return accountService.logout();
    }
}
